package com.controller;

import com.bean.User;
import com.service.imp.UserServiceImp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.logging.Handler;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        解决中文乱码
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
//        获取请求参数
        String um = req.getParameter("username");
        String pd = req.getParameter("password");
//        手动封装对
        User user = new User(0, um, pd);
        UserServiceImp service = new UserServiceImp();
        String s = service.selectByName(user);
//        如果登录成功:跳转到首页
//        如果登录失败:跳转到登录页面
        if ("登录成功".equals(s)){
//            跳转到登录页面
            req.getRequestDispatcher("/index.jsp").forward(req,resp);
        }else {
            req.setAttribute("msg",s);
//            跳转到登录页面
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
